package com.itheima.springbootcondition.condition;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

import java.util.Map;

/**
 * @author zero
 * @date 2020/6/11
 */
public class ClassCondition implements Condition {

    /**
     *
     * @param conditionContext 上下文对象
     * @param annotatedTypeMetadata  注解元对象,可以用于获取注解定义的值
     */



    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {

       /* //条件:如果存在jedis就导入user对象
        boolean flag = false;
        try {
            Class<?> cls = Class.forName("redis.clients.jedis.Jedis");

           flag = true;
        } catch (ClassNotFoundException e) {
           e.printStackTrace();
        }
        return flag;*/


        //条件:如果注解指定值value的值导入user对象

        Map<String, Object> map = annotatedTypeMetadata.getAnnotationAttributes(ConditionOnClass.class.getName());
        String[] values = (String[]) map.get("value");
        boolean flag = false;
        try {
            for (Object value : values) {
                Class<?> cls = Class.forName((String) value);
            }
            flag = true;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return flag;
    }
}
